<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons");
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
    text: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({ expectedData, actualData } = {}) {
      let step = 0;
      let text = this.text;
      let rateStr = "";
      if (text == "意向率") {
        rateStr = "%";
      }
      if (expectedData.length <= 7) {
        step = 0;
      } else if (expectedData.length > 7 && expectedData.length <= 30) {
        step = Math.floor(expectedData.length / 7);
      }
      // 取开始时间和当前时间计算
      this.chart.setOption({
        xAxis: {
          boundaryGap: false,
          axisTick: {
            show: false,
          },
          axisLabel: {
            interval: step,
            rotate: 0,
          },
          data: expectedData,
        },
        grid: {
          left: 10,
          right: 40,
          bottom: 20,
          top: 30,
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
          padding: [5, 10],
          formatter: function (params) {
            let firstParams = params[0];
            return (
              text +
              "<br>" +
              firstParams.data +
              rateStr +
              "<br>" +
              firstParams.name
            );
          },
        },
        yAxis: {
          type: "value",
          axisTick: {
            show: false,
          },
          axisPointer: {
            label: {
              formatter: function (params) {
                return parseInt(params.value) + rateStr;
              },
            },
          },
        },
        legend: {},
        series: [
          {
            itemStyle: {
              normal: {
                lineStyle: {
                  color: "#83AAFB",
                  width: 2,
                },
              },
            },
            smooth: true,
            type: "line",
            data: actualData,
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: "rgba(80,141,255,0.39)",
                  },
                  {
                    offset: 0.34,
                    color: "rgba(56,155,255,0.25)",
                  },
                  {
                    offset: 1,
                    color: "rgba(38,197,254,0.00)",
                  },
                ]),
              },
            },
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          },
        ],
      });
    },
  },
};
</script>
